/**
 * 管理员角色下拉
 */

import { Select, SelectProps } from 'antd';
import { memo, useImperativeHandle, useState } from 'react';

import { IRoleRestFillRefHandler } from '../../types';
import { useGetlawsTypeAll } from '../../service/laws-type-service';

interface ILawsSelectProps extends SelectProps<number> {
    handleChange: (value: number) => void;
    onRef?: React.MutableRefObject<IRoleRestFillRefHandler | undefined>;
}

export const LawsSelect = ({ handleChange, onRef }: ILawsSelectProps) => {
    const { isLoading, isError, data: dataSource } = useGetlawsTypeAll();
    const [val, setVal] = useState<number>();
    const onChange = (value: number) => {
        setVal(value);
        handleChange(value);
    };

    /**向外暴露的重置方法 */
    useImperativeHandle(onRef, () => ({
        rest: () => {
            setVal(undefined);
        },
        fill: (sysRoleId) => {
            setVal(sysRoleId);
        },
    }));
    return (
        <Select placeholder={'法律分类'} value={val} onChange={onChange} loading={isLoading}>
            {isError
                ? null
                : dataSource?.map((data) => {
                    return (
                        <Select.Option key={data.lawsTypeId} value={data.lawsTypeId}>
                            {data.lawsTypeName}
                        </Select.Option>
                    );
                })}
        </Select >
    );
};


export default memo(LawsSelect);